package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameFlorist;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.florist.FloristSecretary;
import com.motu.monstercity.server.game.commondata.florist.FloristTask;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.UserFlorist;
import com.motu.monstercity.server.game.userdata.UserFloristTask;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import io.vertx.core.json.JsonArray;

import java.util.List;

public class FloristHandler {

    // CMD_GAME_FLORIST_INFO = 71901; // 信息界面
    public static Object CMD_GAME_FLORIST_INFO(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        UserFlorist userFlorist = FloristManager.getUserFlorist(userId);
        FloristManager.initTaskList(userFlorist);
        List<UserFloristTask> taskList = FloristManager.getUserFloristTaskList(userId);
        List<UserFloristTask> showTaskList = FloristManager.getShowTaskList(userFlorist, taskList);
        CsGameFlorist.CSGameFloristInfoResponse.Builder response = CsGameFlorist.CSGameFloristInfoResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        for (UserFloristTask userFloristTask : showTaskList) {
            ProtoDataUtils.updatePBUserData(pbUserData, userFloristTask, true);
        }
        ProtoDataUtils.updatePBUserData(pbUserData, userFlorist, true);
        return response;
    }

    // CMD_GAME_FLORIST_COMPLETE_TASK = 71902; // 完成任务
    public static Object CMD_GAME_FLORIST_COMPLETE_TASK(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        CsGameFlorist.CSGameFloristCompleteTaskRequest request = CsGameFlorist.CSGameFloristCompleteTaskRequest.parseFrom(clientParam.getData());
        int taskId = request.getTaskId();
        FloristTask floristTask = FloristManager.getFloristTask(taskId);
        if (floristTask == null) {
            return ErrorWord.NOT_DATA;
        }
        UserFlorist userFlorist = FloristManager.getUserFlorist(userId);
        JsonArray taskArray = userFlorist.getTaskArray();
        if (!taskArray.contains(taskId)) {
            return ErrorWord.NOT_DATA_1;
        }
        UserFloristTask userFloristTask = FloristManager.getUserFloristTask(userId, taskId);
        if (userFloristTask == null) {
            return ErrorWord.WRONG_STATUS;
        }
        if (userFloristTask.getIsRecv() == 1) { // 已完成任务
            return ErrorWord.REPEATED_OPERATION;
        }
        if (userFloristTask.getCurP() < floristTask.getTaskPar()) { // 任务未达标
            return ErrorWord.INVALID_OPERATION;
        }
        UserInfo userInfo = GameUser.getUserInfo(userId);
        CsGameFlorist.CSGameFloristCompleteTaskResponse.Builder response = CsGameFlorist.CSGameFloristCompleteTaskResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        String reward = floristTask.getReward();
        RewardManager.addReward(userInfo, reward, pbUserData, LogType.FLORIST_TASK_COMPLETE, taskId);
        userFloristTask.putIsRecv((byte) 1);
        userFloristTask.update();
        List<UserFloristTask> taskList = FloristManager.getUserFloristTaskList(userId);
        boolean replace = FloristManager.completeTask(userFlorist, userFloristTask, taskList);
        if (replace) {
            userFlorist.update();
        }
        ProtoDataUtils.updatePBUserData(pbUserData, userFlorist);
        List<UserFloristTask> showTaskList = FloristManager.getShowTaskList(userFlorist, taskList);
        for (UserFloristTask showTask : showTaskList) {
            ProtoDataUtils.updatePBUserData(pbUserData, showTask, true);
        }
        return response;
    }

    // CMD_GAME_FLORIST_UNLOCK_SECRETARY = 71903; // 解锁秘书
    public static Object CMD_GAME_FLORIST_UNLOCK_SECRETARY(RequestParam clientParam) throws Exception {
        long userId = clientParam.getUserId();
        CsGameFlorist.CSGameFloristUnlockSecretaryRequest request = CsGameFlorist.CSGameFloristUnlockSecretaryRequest.parseFrom(clientParam.getData());
        int secretaryId = request.getSecretaryId();
        FloristSecretary floristSecretary = FloristManager.getFloristSecretary(secretaryId);
        if (floristSecretary == null) {
            return ErrorWord.NOT_DATA;
        }
        UserFlorist userFlorist = FloristManager.getUserFlorist(userId);
        JsonArray secretaryIds = userFlorist.getSecretariesArray();
        if (!secretaryIds.contains(secretaryId)) { // 未开始
            return ErrorWord.NOT_DATA_1;
        }
        JsonArray secretaryUnlockArray = userFlorist.getSecretaryUnlockArray();
        if (secretaryUnlockArray.contains(secretaryId)) {
            return ErrorWord.REPEATED_OPERATION; // 已解锁过了
        }

        UserInfo userInfo = GameUser.getUserInfo(userId);
        CsGameFlorist.CSGameFloristUnlockSecretaryResponse.Builder response = CsGameFlorist.CSGameFloristUnlockSecretaryResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        List<RewardItem> costItems = CommonUtils.takeReawrdItemFromStr(floristSecretary.getNeed());
        if (!BagManager.checkNeedItemNum(userInfo, costItems)) {
            return ErrorWord.ITEM_LACK; // 道具不足
        }
        RewardManager.subReward(userInfo, costItems, pbUserData, LogType.FLORIST_SECRETARY_UNLOCK, secretaryId);
        RewardManager.addReward(userInfo, secretaryId, 1, pbUserData, LogType.FLORIST_SECRETARY_UNLOCK);
        secretaryUnlockArray.add(secretaryId);
        userFlorist.putSecretaryUnlock(secretaryUnlockArray.toString()); // 这里先更新一下已解锁的秘书，下面的方法检测是否解锁新秘书时才是最新数据
        FloristManager.addNewSecretary(userFlorist);
        userFlorist.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userFlorist);
        return response;
    }
}
